import { Link } from 'react-router-dom'
import {
  Card,
  Breadcrumb,
  Form,
  Button,
  Radio,
  DatePicker,
  Select,
  Table,
  Tag,
  Space,
} from 'antd'
import { useDispatch, useSelector } from 'react-redux'
import { getChannels, getArticles } from '@/store/action/article'
import { EditOutlined, DeleteOutlined } from '@ant-design/icons'
import 'moment/locale/zh-cn'
import img404 from '@/assets/error.png'
import { useEffect } from 'react'
// 国际化设置
// import locale from 'antd/es/date-picker/locale/zh_CN'

const { Option } = Select
const { RangePicker } = DatePicker

// 优化文章状态的处理
const articleStatus = {
  0: { color: 'yellow', text: '草稿' },
  1: { color: '#ccc', text: '待审核' },
  2: { color: 'green', text: '审核通过' },
  3: { color: 'red', text: '审核失败' },
}

const Article = () => {
  /* 需求：获取频道数据，绑定到表单筛选区域下拉框中
      1.定义异步action获取后台频道数据，存到redux
      2.从redux获取数据渲染
  */
  const dispatch = useDispatch()
  const { channels, page, pageSize, count, list } = useSelector(
    (state) => state.article
  )
  useEffect(() => {
    dispatch(getChannels())
    dispatch(getArticles({}))
  }, [dispatch])

  const onSearch = (values) => {
    const { status, channel_id, date } = values
    // 将表单中选中数据，组装成接口需要的数据格式，然后，传递给接口
    const params = { channel_id }
    // 处理状态
    if (status !== -1) {
      params.status = status
    }
    // 日期范围
    if (typeof date !== 'undefined' && date !== null) {
      params.begin_pubdate = date[0].format('YYYY-MM-DD HH:mm:ss')
      params.end_pubdate = date[1].format('YYYY-MM-DD HH:mm:ss')
    }

    dispatch(getArticles(params))
  }

  const delArticle = (id) => {
    console.log('删除', id)
  }

  // 结合后台当前列表接口的文档配置
  const columns = [
    {
      title: '封面',
      dataIndex: 'cover',
      render: (cover) => {
        return <img src={cover || img404} width={200} height={150} alt="" />
      },
    },
    {
      title: '标题',
      dataIndex: 'title',
      width: 220,
    },
    {
      title: '状态',
      dataIndex: 'status',
      render: (data) => {
        // 方式二：使用对象属性的访问，替代了 原来的 if-else 或 switch-case 判断
        const tagData = articleStatus[data]
        return <Tag color={tagData.color}>{tagData.text}</Tag>
      },
    },
    {
      title: '发布时间',
      dataIndex: 'pubdate',
    },
    {
      title: '阅读数',
      dataIndex: 'read_count',
    },
    {
      title: '评论数',
      dataIndex: 'comment_count',
    },
    {
      title: '点赞数',
      dataIndex: 'like_count',
    },
    {
      title: '操作',
      render: (data) => {
        return (
          <Space size="middle">
            <Button type="primary" shape="circle" icon={<EditOutlined />} />
            <Button
              type="primary"
              danger
              shape="circle"
              icon={<DeleteOutlined />}
              onClick={() => {
                delArticle(data.id)
              }}
            />
          </Space>
        )
      },
    },
  ]

  // 分页切换时间
  const changePage = (page, pageSize) => {
    // 分页数据
    const params = {
      page,
      per_page: pageSize,
    }
    dispatch(getArticles(params))
  }
  return (
    <div>
      <Card
        title={
          // 面包屑导航
          // separator 表示两个 面包屑名称 之间的分隔符
          <Breadcrumb separator=">">
            <Breadcrumb.Item>
              <Link to="/home">首页</Link>
            </Breadcrumb.Item>
            <Breadcrumb.Item>内容管理</Breadcrumb.Item>
          </Breadcrumb>
        }
        style={{ marginBottom: 20 }}>
        <Form initialValues={{ status: -1 }} onFinish={onSearch}>
          <Form.Item label="状态" name="status">
            {/* 单选:选中文章的状态 */}
            <Radio.Group>
              <Radio value={-1}>全部</Radio>
              <Radio value={0}>草稿</Radio>
              <Radio value={1}>待审核</Radio>
              <Radio value={2}>审核通过</Radio>
              <Radio value={3}>审核失败</Radio>
            </Radio.Group>
          </Form.Item>

          <Form.Item label="频道" name="channel_id">
            {/* 下拉选择：文章频道 */}
            <Select placeholder="请选择文章频道">
              {channels.map((item) => (
                <Option key={item.id} value={item.id}>
                  {item.name}
                </Option>
              ))}
            </Select>
          </Form.Item>

          <Form.Item label="日期" name="date">
            {/* 时间范围：文章发布时间 */}
            {/* <RangePicker locale={locale}></RangePicker> */}
            <RangePicker></RangePicker>
          </Form.Item>

          <Form.Item>
            {/* 提交：筛选文章列表数据 */}
            <Button type="primary" htmlType="submit">
              筛选
            </Button>
          </Form.Item>
        </Form>
      </Card>
      <Card title={`根据筛选条件共查询到 ${count} 条结果：`}>
        <Table
          columns={columns}
          dataSource={list}
          rowKey="id"
          pagination={{
            position: ['bottomCenter'],
            showSizeChanger: true,
            total: count,
            current: page,
            pageSize,
            onChange: changePage,
          }}
        />
      </Card>
    </div>
  )
}

export default Article
